package com.itheima.accountingdb.controller;

import com.itheima.accountingdb.pojo.PageBean;
import com.itheima.accountingdb.pojo.Result;
import com.itheima.accountingdb.pojo.Transaction;
import com.itheima.accountingdb.service.TransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author panweiwei
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/transactions")
@CrossOrigin
@Tag(name = "账单管理", description = "账单管理相关接口")
public class TransactionController {

    private final TransactionService transactionService;

    /**
     * 查询所有账单
     * @param page
     * @param size
     * @param startDate
     * @param endDate
     * @param type
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有账单",deprecated = false)
    public Result<PageBean<Transaction>> list(Integer page, Integer size, String startDate, String endDate, String type, String categoryId,Long isDeleted) {
        PageBean<Transaction> pb = transactionService.list(page, size, startDate, endDate, type, categoryId,isDeleted);
        //输出日志
        System.out.println(pb);
        return Result.success(pb);
    }

    /**
     * 添加账单
     * @param transaction
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加账单",deprecated = false)
    @Parameter(description = "账单信息")
    public Result<Transaction> add(@RequestBody Transaction transaction){
      Transaction tr =  transactionService.add(transaction);
      if (tr == null){
          return Result.error("添加失败");
      }
        return Result.success(tr);
    }

    /**
     * 修改账单
     * @param transaction
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<Transaction> update (@PathVariable Integer id,@RequestBody Transaction transaction){
        System.out.println("id:"+id);
        Transaction tr =  transactionService.update(transaction,id);
        if (tr == null){
            return Result.error("修改失败");
        }
        return Result.success(tr);
    }

    /**
     * 删除账单
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        transactionService.delete(id);
        return Result.success();
    }
}
